package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.SearchProduct;

import entities.Product;

/**
 * Servlet implementation class AddToCart
 */
@WebServlet("/AddToCart")
public class AddToCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddToCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//PrintWriter out = response.getWriter();
		
		response.setContentType("html/text");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession(true);
		String productid = request.getParameter("Productid");
		List productids = (List)session.getAttribute("Productids");
		productids.add(productid);
		session.setAttribute("Productids", productids);
		response.sendRedirect("showorder.jsp");
		
		//out.print("hello");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("html/text");
		response.getWriter().write("success");
		HttpSession session = request.getSession(true);
		/*long productid = Long.parseLong(request.getAttribute("value").toString());
		ArrayList productids = (ArrayList)session.getAttribute("Productids");
		productids.add(productid);
		session.setAttribute("Productids", productids);*/
		
		//String pid = request.getAttribute("value").toString();
		String pid = request.getParameter("Productid");
		Product p = SearchProduct.searchByID(pid);
		session.setAttribute("Product", p);
		response.sendRedirect("ordercart.jsp");
		
		
	}

}
